﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Utility.Json
{
    /// <summary>
    /// 角色权限创建树
    /// </summary>
    public class JsonRoleAuthTree
    {
        static DataTable AuthTable, moduleTable_YX, moduleOperateStr, moduleOperateTable_YX;
        static string strOperateName, strTextName;

        #region 创建组织机构Tree
        /// <summary>
        /// 由DataTable 生成树类
        /// </summary>
        /// <param name="ti">要装载的树类根节点</param>
        /// <param name="dt">数据表</param>
        /// <param name="sId">父类ID</param>
        /// <returns></returns>
        public static void GetTreeModuleItemFromDataTable(TreeItem ti, DataTable _AuthTable, DataTable _moduleTable_YX, DataTable _moduleOperateStr, DataTable _moduleOperateTable_YX, string sId)
        {
            AuthTable = _AuthTable; moduleTable_YX = _moduleTable_YX; moduleOperateStr = _moduleOperateStr; moduleOperateTable_YX = _moduleOperateTable_YX;
            strTextName = "ModuleName";
            strOperateName = "OperateName";
            CreateOne(ti, sId, true);
            AuthTable.Dispose(); moduleTable_YX.Dispose(); moduleOperateStr.Dispose(); moduleOperateTable_YX.Dispose();
            AuthTable = moduleTable_YX = moduleOperateStr = moduleOperateTable_YX = null;
            strOperateName = strTextName = null;
        }

        public static void GetTreeModuleItemFromDataTable(TreeItem ti, DataTable _moduleStr, DataTable _moduleOperateStr, string sId)
        {
            AuthTable = _moduleStr; moduleOperateStr = _moduleOperateStr;
            strTextName = "ModuleName";
            strOperateName = "OperateName";
            CreateOne(ti, sId, false);
            AuthTable.Dispose(); moduleOperateStr.Dispose();
            AuthTable = moduleOperateStr = null;
            strOperateName = strTextName = null;
        }

        private static void CreateOne(TreeItem ti, string sId, bool IsSetOrLook)
        {
            TreeItem ti0;
            string parent = string.Empty;
            ti.ChildNodes = new List<TreeItem>();
            DataRow[] drs0 = AuthTable.Select("FatherID='" + sId + "'");
            foreach (DataRow dr in drs0)
            {
                parent = dr["ModuleID"].ToString();
                if (IsSetOrLook)
                {
                    DataRow[] drYx = moduleTable_YX.Select("ID='" + dr["ID"].ToString() + "'");
                    if (drYx.Length > 0)
                    {
                        ti0 = new TreeItem() { id = dr["ID"].ToString(), value = dr["ID"].ToString(), text = dr[strTextName].ToString(), showcheck = true, checkstate = 1, isexpand = true };
                    }
                    else
                    {
                        ti0 = new TreeItem() { id = dr["ID"].ToString(), value = dr["ID"].ToString(), text = dr[strTextName].ToString(), showcheck = true, isexpand = true };
                    }
                }
                else
                {
                    ti0 = new TreeItem() { id = dr["ID"].ToString(), value = dr["ID"].ToString(), text = dr[strTextName].ToString(), showcheck = false, isexpand = false };
                }
                CreateNode(ti0, parent, IsSetOrLook);
                ti.Add(ti0);
            }
        }
        private static void CreateOpenNode(TreeItem ti, string sId, bool IsSetOrLook)
        {
            TreeItem ti0;
            DataRow[] drs0 = moduleOperateStr.Select("ModuleID='" + sId + "'");
            if (drs0.Length > 0)
            {
                if (ti.ChildNodes == null || ti.ChildNodes.Count == 0)
                {
                    ti.ChildNodes = new List<TreeItem>();
                }
                int i = 0;
                foreach (DataRow dr in drs0)
                {
                    i++;
                    if (IsSetOrLook)
                    {
                        DataRow[] drYx = moduleOperateTable_YX.Select("operateid='" + dr["OperateID"].ToString() + "' and moduleid='" + sId + "'");
                        if (drYx.Length > 0)
                        {
                            ti0 = new TreeItem() { id = sId + "_" + i.ToString(), value = sId + "$" + dr["OperateID"].ToString(), text = dr[strOperateName].ToString(), showcheck = true, checkstate = 1 };
                        }
                        else
                        {
                            ti0 = new TreeItem() { id = sId + "_" + i.ToString(), value = sId + "$" + dr["OperateID"].ToString(), text = dr[strOperateName].ToString(), showcheck = true };
                        }
                    }
                    else
                    {
                        ti0 = new TreeItem() { id = sId + "_" + i.ToString(), value = sId + "$" + dr["OperateID"].ToString(), text = dr[strOperateName].ToString(), showcheck = false };
                    }
                    ti.Add(ti0);
                }
            }
        }
        private static void CreateNode(TreeItem ti, string sId, bool IsSetOrLook)
        {
            TreeItem ti0;
            string parent = string.Empty;
            ti.ChildNodes = new List<TreeItem>();
            DataRow[] drs0 = AuthTable.Select("FatherID='" + sId + "'");
            foreach (DataRow dr in drs0)
            {
                parent = dr["ModuleID"].ToString();
                if (IsSetOrLook)
                {
                    DataRow[] drYx = moduleTable_YX.Select("ID='" + dr["ID"].ToString() + "'");
                    if (drYx.Length > 0)
                    {
                        ti0 = new TreeItem() { id = dr["ID"].ToString(), value = dr["ID"].ToString(), text = dr[strTextName].ToString(), showcheck = true, checkstate = 1 };
                    }
                    else
                    {
                        ti0 = new TreeItem() { id = dr["ID"].ToString(), value = dr["ID"].ToString(), text = dr[strTextName].ToString(), showcheck = true };
                    }
                }
                else
                {
                    ti0 = new TreeItem() { id = dr["ID"].ToString(), value = dr["ID"].ToString(), text = dr[strTextName].ToString(), showcheck = false };
                }
                CreateNode(ti0, parent, IsSetOrLook);
                CreateOpenNode(ti0, dr["ID"].ToString(), IsSetOrLook);
                ti.Add(ti0);
            }
        }
        #endregion
    }
}
